package com.ddone.finance.controller.api;

import com.ddone.base.util.JwtUtils;
import com.ddone.common.exception.BusinessException;
import com.ddone.common.result.ResponseEnum;
import com.ddone.common.util.HttpClientUtils;
import com.ddone.finance.config.WxLoginProperties;
import com.ddone.finance.pojo.entity.UserInfo;
import com.ddone.finance.service.UserInfoService;
import com.ddone.finance.service.WxLoginService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author ddone
 * @date 2024/4/17-23:01
 */
// @CrossOrigin
@Controller
@RequestMapping("/core/api/wx")
@Api(tags = {"微信登录"})
@Slf4j
public class WxLoginController {

    @Resource
    private WxLoginService wxLoginService;

    @Resource
    private WxLoginProperties wxLoginProperties;

    @ApiOperation("打开微信登录二维码")
    @GetMapping("/login")
    public String login(HttpSession session) {
        String qrcodeUrl = wxLoginService.getQrcodeUrl(session);
        return "redirect:" + qrcodeUrl;

    }

    @ApiOperation("微信授权后回调")
    @GetMapping("/callback")
    public String callback(String code,
                           String state,
                           HttpSession session,
                           HttpServletRequest request) throws JsonProcessingException {
        String token = wxLoginService.callBack(code, state, session,request);
        return "redirect:"+wxLoginProperties.getFeDirectUri()+"?token=" + token;
    }
}
